#!/usr/bin/env python
# _*_ coding: utf-8 _*_
# @Time    : 2018/5/18 0018 上午 11:30
# @Author  : Jython <ijython@qq.com>
# @File    : Grequest.py

import json
from flask import make_response
from .Gview import Gview

class Grequest( ):
	"""
	http统一钩子方法
	"""

	@staticmethod
	def beforeRequest():
		"""
		http请求前置钩子方法
		:return:
		"""
		return True

	@staticmethod
	def afterRequest( response ):
		"""
		http后置钩子方法
		:param response:
		:return:
		"""
		obj = {}
		obj[ 'code' ] = response.status_code
		data = response.data.decode('utf-8')
		try:
			data = json.loads(data)
			if 'code' in data.keys( ):
				obj = data
			else:
				message = data.get('message')
				if message:
					del data[ 'message' ]
				obj[ 'data' ] = data
				obj[ 'message' ] = "" if not message else message
		except:
			obj[ 'message' ] = "" if not message else message
		finally:
			response = make_response(obj)
		return response
